package com.william.yun.yunliwuli.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.william.yun.yunliwuli.bean.Login;
import com.william.yun.yunliwuli.service.BaseService;
import com.william.yun.yunliwuli.service.CoreService;
import com.william.yun.yunliwuli.constant.YunliwuliConstant;
import com.william.yun.yunliwuli.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;

import java.io.IOException;
import java.util.HashMap;

@Slf4j
public class BaseServiceImpl extends CoreService implements BaseService {
    @Override
    public JSONObject login(String loginname, String password)  {
        JSONObject jsonObject = new JSONObject();
        try {

            Login loginRequest = new Login( loginname, password);
            HashMap<String, String> header = new HashMap<>();
            header.put(HttpHeaders.CONTENT_TYPE, YunliwuliConstant.headerJson);
            String post = new HttpUtils().post(LOGIN_API, JSONObject.parseObject(JSON.toJSONString(loginRequest)), header);
            if(StringUtils.isEmpty(post)){
                jsonObject.put("status",  500);
                jsonObject.put("message",  "网络错误");
                return jsonObject;
            }
            return JSONObject.parseObject(post);
        }catch (IOException ioException){
            //报错处理
            jsonObject.put("status", "500");
            jsonObject.put("message", ioException.getMessage());
        }
        return jsonObject;
    }

    /**
     * 获取使用场景
     *
     * @param token
     * @return
     */
    @Override
    public JSONObject senceUsing(String token) {
        return getJsonResult(null, token, SENCEUSING);
    }

    /**
     * 更改商品、价签、模板绑定关系
     *
     * @param body
     * @param token
     */
    @Override
    public JSONObject getWarningInformation(JSONObject body, String token) {
        return getJsonResult(body, token, WARNINGINFORMATION_URL);
    }




/*    @Test
    public void run(){
        JSONObject login = new BaseServiceImpl().login("jieyousheng", "123456");
        log.info("login:{}", login);
        //new BaseServiceImpl().senceUsing();
    }*/


}
